package com.cloudcc.mobile.view.fragment;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ListView;
import butterknife.Bind;

import com.cloudcc.cloudframe.bus.EventEngine;
import com.cloudcc.cloudframe.bus.IEventLife;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrDefaultHandler;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrFrameLayout;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.PtrHandler;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.header.DefaultMaterialHeader;
import com.cloudcc.cloudframe.ui.viewgroup.refresh.header.MaterialHeader;
import com.cloudcc.cloudframe.util.DensityUtils;
import com.cloudcc.mobile.R;
import com.cloudcc.mobile.adapter.BeauRelationFollowAdapter;
import com.cloudcc.mobile.event.BeauEventList.RelateionFollowListEvent;
import com.cloudcc.mobile.presenter.BeauPresenter;
import com.cloudcc.mobile.util.ViewUtils;
import com.cloudcc.mobile.view.main.fragment.BaseListFragment;
import com.cloudcc.mobile.weight.EmptyLayout;
import com.cloudcc.mobile.widget.CloudCCTitleBar;
import com.cloudcc.mobile.widget.CloudCCTitleBar.OnTitleBarClickListener;

public class BeauFollowHistoryFragment extends BaseListFragment implements
		IEventLife, PtrHandler, OnTitleBarClickListener {

	private BeauPresenter mBeauPresenter = new BeauPresenter();

	private String layoutId;
	private String recoredId;
	private String relatedlistId;

	@Bind(R.id.listview)
	ListView mListView;

	@Bind(R.id.headerbar)
	CloudCCTitleBar headerbar;
	
	EmptyLayout empty_loading;

	@Bind(R.id.load_more_list_view_ptr_frame)
	PtrFrameLayout mRefreshLayout;

	private BeauRelationFollowAdapter mAdapter;

	@Override
	public void init() {
		register();
		empty_loading = new EmptyLayout(mContext);
		empty_loading.setLayoutParams(new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		ViewUtils.addListViewEmptyView(mContext, mListView, empty_loading);
		initHeader();
		initRefresh();
		initRequestData();
	}

	private void initHeader() {
		Bundle bundle = getArguments();
		if (bundle != null) {
			layoutId = bundle.getString("layoutId", "");
			recoredId = bundle.getString("recordId", "");
			relatedlistId = bundle.getString("relatedlistId", "");
			headerbar.setTitle(bundle.getString("name", "字段跟踪"));
		}
		headerbar.setOnTitleBarClickListener(this);

	}

	/**
	 * 初始化刷新
	 */
	protected void initRefresh() {
		mRefreshLayout.setLoadingMinTime(1000);
		mRefreshLayout.setPtrHandler(this);

		final MaterialHeader header = new DefaultMaterialHeader(getActivity());
		int[] colors = getResources().getIntArray(R.array.google_colors);
		header.setColorSchemeColors(colors);
		header.setLayoutParams(new PtrFrameLayout.LayoutParams(-1, -2));
		header.setPadding(0, DensityUtils.dpToPixel(mContext, 10), 0,
				DensityUtils.dpToPixel(mContext, 5));
		header.setPtrFrameLayout(mRefreshLayout);
		mRefreshLayout.setHeaderView(header);
		mRefreshLayout.addPtrUIHandler(header);
		mRefreshLayout.setResistance(5.0f);
	}

	/**
	 * 自动开启刷新请求数据
	 */
	private void initRequestData() {
		mRefreshLayout.postDelayed(new Runnable() {
			@Override
			public void run() {
				mRefreshLayout.autoRefresh(true);
			}
		}, 150);
	}

	public void onEventMainThread(RelateionFollowListEvent event) {
		mRefreshLayout.refreshComplete();
		empty_loading.hide();
		headerbar.completeProgress();
		if ((event.isError()||event.getData()==null)) {
			empty_loading.empty();
		}
		if (mAdapter == null) {
			mAdapter = new BeauRelationFollowAdapter(mContext);
			mListView.setAdapter(mAdapter);
		}
		mAdapter.changeData(event.getData().relationData);

	}

	@Override
	public void requestData() {
		mBeauPresenter.getBeauRelationFollows(layoutId, recoredId,
				relatedlistId);
	}

	@Override
	public int getLayoutId() {
		return R.layout.beau_relation_history;
	}

	@Override
	public void onDestroy() {
		unRegister();
		super.onDestroy();
	}

	@Override
	public void register() {
		EventEngine.register(this);
	}

	@Override
	public void unRegister() {
		EventEngine.uregister(this);
	}

	@Override
	public boolean checkCanDoRefresh(PtrFrameLayout frame, View arg1,
			View header) {
		return PtrDefaultHandler.checkContentCanBePulledDown(frame, mListView,
				header);
	}
	@Override
	public void onRefreshBegin(PtrFrameLayout arg0) {
		onRefreshData();
	}

	@Override
	public void onClickLeft(View v) {
		getActivity().finish();
	}

	@Override
	public void onClickRight(View v) {

	}
}
